package net 
{
	/**
	 * ...
	 * @author Herbs
	 */
	public class LoaderQueue 
	{
		private var queues:Vector.<LoaderAdapter>;
		private var wcLoader:WebClient;
		private var minIndex:int = 0;
		private var maxIndex:int = 0;
		public var isLoading:Boolean = false;
		public var currentAda:LoaderAdapter;
		
		public function LoaderQueue() 
		{
			queues = new Vector.<LoaderAdapter>();
			
		}
		
		
		public function up(ada:LoaderAdapter):void 
		{
			if (ada.loaded) return;
			
			ada.index = ++maxIndex;
		}
		
		public function down(ada:LoaderAdapter):void 
		{
			if (ada.loaded) return;
			ada.index = --minIndex;
		}
		
		public function add(adapter:LoaderAdapter):void 
		{
			if (adapter.index == 0)
			{
				adapter.index = minIndex--;
			}
			else 
			{
				if (adapter.index < minIndex)
					minIndex = adapter.index;
				if (adapter.index > maxIndex)
					maxIndex = adapter.index;
			}
			
			queues.push(adapter);
			
			if (!isLoading)
			{
				loadItem();
			}
		}
		
		public function del(ada:LoaderAdapter):void 
		{
			if (currentAda == ada)
				return;
			for (var i:int = queues.length - 1; i > -1; i-- )
			{
				if (queues[i] == ada)
				{
					queues.splice(i, 1);
					return;
				}
				
			}
		}
		
		/**
		 * 停止
		 * @param	b	是否终止当前正在进行的下载
		 */
		public function stop(b:Boolean = true):void 
		{
			if (b)
			{
				queues.length = 0;
				if (isLoading)
					wcLoader.close();
				isLoading = false;
			}
			else 
			{
				queues.length = 0;
				if (currentAda != null)
					queues.push(currentAda);
			}
		}
		
		
		private function loadItem():void 
		{
			
			
			var queueSize:int = queues.length;
		
			if (queueSize > 0)
			{
				

				//queues.sort(sortByIndex);
				

				var ada:LoaderAdapter = queues[0];
				currentAda = ada;
				
				wcLoader = new WebClient();
				wcLoader.fnDownloadComplete = onComplete;
				wcLoader.fnDownloadError = onError;
				wcLoader.fnDownloadProgress = ada.fnProgress;
				wcLoader.fnDownloadTimeOut = onTimeout;
				
				wcLoader.params = ada.params;
				ada.loaded = true;
				isLoading = true;
				
				switch(ada.type)
				{
					case WebClient.STRING:
						wcLoader.downloadString(ada.addr);
						break;
					case WebClient.BINARY:
						wcLoader.downloadData(ada.addr);
						break;
					case WebClient.IMAGE:
						wcLoader.downloadImage(ada.addr, ada.async);
						break;
					case WebClient.TXML:
						wcLoader.downloadXML(ada.addr);
						break;
					case WebClient.MOVIECLIP:
						wcLoader.downloadSWF(ada.addr);
						break;
				}
			}
			
			
		}
		
		private function onError(...args):void 
		{
			isLoading = false;
			queues[0].loaded = true;
			
			queues[0].fnError.apply(null, args);
			queues.pop();
			if (queues.length > 0)
				loadItem();
			else 
				loadComplete();
		}
		
		private function loadComplete():void 
		{
			
		}
		
		private function onComplete(data:*, key:*):void 
		{
			isLoading = false;
			queues[0].loaded = true;
			queues[0].fnComplete.apply(null, [data, key]);
			queues.shift();
			
			if (queues.length > 0)
				loadItem();
			else 
				loadComplete();
		}
		
		private function onTimeout():void 
		{
			isLoading = false;
			queues[0].loaded = true;
			queues[0].fnTimeout.apply(null);
			queues.pop();
			if (queues.length > 0)
				loadItem();
			else 
				loadComplete();
		}
		
		
		private function sortByIndex(a1:LoaderAdapter, a2:LoaderAdapter):int 
		{
			if (a1.index > a2.index)
				return -1;
			else if (a1.index < a2.index)
				return 1;
			else
				return 0;
		}
		
	}

}